package cn.liujianbo.example.base.rexg;

/**
 * 正则表达式
 * @author 刘建波
 * @date 2021-09-24 11:26
 */
public class Demo {
    public static void main(String[] args) {
        p("abc.matches(abc)","abc".matches("abc"));
        //  . 匹配一个任意字符
        p("abc.match(...)", "abc".matches("..."));
        // \w 单词字符：[a-zA-Z_0-9]     * 零次或多次     ? 零次或一次     + 一次或多次     ｛N｝ 出现 N 次
        p("abc.matches(\\\\w*)", "abc".matches("\\w*"));
        p("abc.matches(\\\\w?)","abc".matches("\\w?"));
        p("abc.matches(\\\\w+)","abc".matches("\\w+"));
        p("abc.matches(\\\\w{3})","a_8".matches("\\w{3}"));
        // \d 数字：[0-9]
        p("214523145234532.matches(\\d{3,100})", "214523145234532".matches("\\d{3,100}"));
        //  .   \  ^ 需要转义  \\.   \\\\   \\^
        p("192.168.0.aaa.matches(\\d{1,3}.d{1,3}.d{1,3}.d{1,3})", "192.168.0.aaa".matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"));
        p("\\.matches(\\)", "\\".matches("\\\\"));
        p("^.matches([^])", "^".matches("[\\^]"));
        p("^.matches(^)", "^".matches("\\^"));
        p("^.matches(^)", "^".matches("^"));
        // 一对方框号对一个字符进行匹配       方框号内的^表示取反   方框号外 ^ 表示行的开头
        p("192.matches([0-2][0-9][0-9])", "192".matches("[0-2][0-9][0-9]"));
        p("a.matches([^abc])", "a".matches("[^abc]"));
        // 并集
        p("A.matches([a-zA-Z])", "A".matches("[a-zA-Z]"));
        p("A.matches([a-z]|[A-Z])", "A".matches("[a-z]|[A-Z]"));
        p("A.matches([a-z[A-Z]])", "A".matches("[a-z[A-Z]]"));
        // 交集
        p("R.matches([A-Z&&[RFG]])", "R".matches("[A-Z&&[RFG]]"));
        //  \s 空白字符：[ \t\n\x0B\f\r]     单独的回车符 ('\r')、 新行（换行）符 ('\n')、 制表符 ('\t')
        p(" \\n\\r\\t.matches(\\\\s{4})", " \n\r\t".matches("\\s{4}"));
        // 非空白字符：[^\s]
        p(" .matches(\\\\S)", " ".matches("\\S"));
        /*
         * POSIX Style 特殊格式 UNIX的标准写法
         * POSIX(可移植性操作系统接口 Portable Operating System Interface of UNIX)
         * \p{Lower} 小写的拼音字符(alphabetic) [a-z]
         * \p{Upper} 大写的拼音字符(alphabetic) [A-Z]
         * \p{ASCII} all ASCII [\x00-\x7F]
         * \p{Alpha} 拼音字符(alphabetic) [\p{Lower}p{Upper}]
         * \p{Digit} 十进制数字（decimal digit）字符 [0-9]
         * \p{Alnum} [\p{Alpha}\p{Digit}]
         */
        p("a.matches(\\\\p{Lower})", "a".matches("\\p{Lower}"));
        //boundary  边界      ^行开头    $行结尾    \b单词边界 前边界后边界   \B 非单词边界
        p("hello sir.matches(^h.*)", "hello sir".matches("^h.*"));
        p("hello sir.matches(.*ir$)", "hello sir".matches(".*ir$"));
        p("hello sir.matches(^h[a-z]{1,3}o\\\\b.*)", "hello sir".matches("^h[a-z]{1,3}o\\b.*"));
        p("hellosir.matches(^h[a-z]{1,3}o\\\\b.*)", "hellosir".matches("^h[a-z]{1,3}o\\b.*"));
    }

    public static void p(String str, Object o) {
        System.out.println(str + "匹配结果： " + o);
    }
    /* 正则表达式
            //whilte lines
             p(" \n".matches("^[\\s&&[^\\n]]*\\n$"));
             p("aaa 8888c".matches(".*\\d{4}."));
             p("aaa 8888c".matches(".*\\b\\d{4}."));
             p("8888c".matches("\\b\\d{4}."));
             p("aaa8888c".matches(".*\\d{4}."));
             p("aaa8888c".matches(".*\\b\\d{4}."));
             //email
             p("asdfasdfsafsf@dsdfsdf.com".matches("[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+"));

             Pattern p = Pattern.compile("\\d{3,5}");
             String s = "123-34345-234-00";
             Matcher m = p.matcher(s);
             p(m.matches());
             m.reset();
             while(m.find()){
                    p(m.start()+"-"+m.end());
             }
             p(m.lookingAt());


             Pattern p = Pattern.compile("java",Pattern.CASE_INSENSITIVE);
             Matcher m = p.matcher("java Java JAVa JaVa IloveJAVA you hateJava  afasdfasdf");
             StringBuffer buf = new StringBuffer();
             int i = 0;
             while(m.find()){
                    i++;
                    if(i%2==0){
                           m.appendReplacement(buf, "java");
                    }else{
                           m.appendReplacement(buf,"JAVA");
                    }
             }
             m.appendTail(buf);
             p(buf);
             */
}
